package com.birdboot.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/*
* 该线程任务负责与指定的客户端进行HTTP交互
* HTTP协议要求浏览器与服务端采取“一问一答”的模式。对此，这里的处理流程分为三步：
* 1.解析请求
* 2.处理请求
* 3.发送请求
* */
public class ClientHandler implements Runnable {
    private Socket socket;
    //ClientHandler new的时候执行构造函数 复制给线程的属性，run方法就可以用了，传参的小方法
    public ClientHandler(Socket socket){
        this.socket = socket;
    }
    @Override
    public void run() {
        try {
            InputStream in = socket.getInputStream();
            int d;
            char pre = 'a',cur = 'a';
            StringBuilder stringBuilder = new StringBuilder();
            while ((d=in.read())!=-1){
                cur = (char)d;
                if(pre == 13&&cur==10){
                    break;
                }
                stringBuilder.append(cur);
                pre = cur;
            }
            String line = stringBuilder.toString().trim();
//            System.out.print(line);
            String method;
            String url;
            String protocol;
            String data[] = line.split("\\s");
            method = data[0];
            url = data[1];
            protocol = data[2];
            System.out.println("method"+method);
            System.out.println("url"+url);
            System.out.println("protocol"+protocol);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
